<?php
/**
 * Created by PhpStorm.
 * Author: 芸签cms www.yunqiancms.com
 * User: sun
 * Date: 2022/7/12
 * Time: 2:12 PM
 */
namespace app\backend\modules\theme\controllers;

use app\backend\controllers\BaseController;
use app\backend\modules\theme\services\ThemeService;
use app\common\models\Theme;
use app\common\models\ThemeFile;
use app\common\facades\Setting;


class ThemeController extends BaseController
{
    public function index(){
        return view("backend.theme.theme.index");
    }
    public function getList(){
        $search = request()->search;
        $list = (new ThemeService())->getList($search);
        return $this->successJson('',$list);
    }

    public function usedLocal(){
        $params = request()->params;
        (new ThemeService())->usedLocal($params);
        return $this->successJson('');
    }
    public function used(){
        $params = request()->params;
        (new ThemeService())->usedMarketTheme($params);
        return $this->successJson('');
    }
    public function update(){
        $id = request()->id;
        (new ThemeService())->update($id);
        return $this->successJson();
    }
    public function changeStatus(){
        /*暂时不开放
         * $id = request()->id;
        (new ThemeService())->changeStatus($id);*/
        return $this->successJson();
    }
    public function setDefault(){
        $id = request()->id;
        (new ThemeService())->setDefault($id);
        return $this->successJson();
    }
    public function getPageList(){
        $themeId= request()->theme_id;
        if(!$themeId){
            return $this->errorJson('请传入模版ID');
        }
        $list = (new ThemeService())->getPageList($themeId,1);
        return $this->successJson('',$list);
    }
    public function pageDetail(){
        return view("backend.theme.theme.design");
    }
    public function getPageDetail(){
        $themeId = request()->theme_id;
        $pageId= request()->page_id;
        if(!$pageId){
            //不传page_id，取该模版首页id
            $indexPage = ThemeFile::site()->select('id')
                ->where('theme_id',$themeId)
                ->where('type',ThemeFile::TYPE_INDEX)
                ->first();
            $pageId = $indexPage['id'];
            if(!$pageId){
                return $this->errorJson('模版ID错误');
            }
        }
        $res = (new ThemeService())->getPageDetail($pageId);
        $themeInfo = Theme::find($themeId);
        $res['theme_type'] = $themeInfo['type'];
        return $this->successJson('',$res);
    }
    public function savePreview(){
        $pageId = request()->page_id;
        $configs = request()->configs;
        $pageInfo = request()->page_info;
        if(!$pageId){
            return $this->errorJson('请传入页面ID');
        }
        (new ThemeService())->savePreview([
            'page_id'=>$pageId,
            'configs'=>$configs,
            'page_info'=>$pageInfo
        ]);
        return $this->successJson();
    }
    //预览
    public function preview(){
        $pageId = request()->page_id;
        $type = intval(request()->type);
        $themeFileInfo = ThemeFile::getBaseInfo($pageId);
        if(!$themeFileInfo['id']||!$themeFileInfo['theme']['id']){
            return $this->errorJson('请传入页面ID');
        }
        $themeInfo = $themeFileInfo['theme'];
        $themeService = new \app\common\services\theme\ThemeService();
        $previewPageData = $themeService->previewPageData($pageId,$type);
        $siteInfo = Setting::get('cms.site');
        $siteInfo['site_logo'] = fmt_media_url($siteInfo['site_logo']);
        $siteInfo['title_icon'] = fmt_media_url($siteInfo['title_icon']);
        $siteInfo['advertisement'] = fmt_media_url($siteInfo['advertisement']);
        $view = $themeInfo['theme'].'.'.$themeFileInfo['file'];
        return view($view, [
            'site_info'=>$siteInfo,
            'vars' =>$previewPageData['datas'],
            'theme_file_info' => $themeFileInfo
        ]);
    }
    public function savePage(){
        $pageId = request()->page_id;
        $configs = request()->configs;
        $pageInfo = request()->page_info;
        if(!$pageId){
            return $this->errorJson('请传入页面ID');
        }
        (new ThemeService())->savePage([
            'page_id'=>$pageId,
            'configs'=>$configs,
            'page_info'=>$pageInfo
        ]);
        return $this->successJson();
    }
}